#定义元素和操作元素的方法
from selenium.webdriver.common.by import By
import time
from Base.basePage import BasePage
from PageObject.login_page import LoginPage

class CartPage(BasePage):

    #元素

    settlementBtn=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[2]/div/div[2]/form/button")   #结算按钮
    addQuantity=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[1]/table/tbody/tr[1]/td[3]/div/span[2]")   #加数量
    subQuantity=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[1]/table/tbody/tr[1]/td[3]/div/span[1]")   #减数量
    selectAll=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[2]/div/div[1]/label/span[2]")   #全选
    deleteAll=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[2]/div/div[1]/a[1]")          #删除所有商品
    deleteOne=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[1]/table/tbody/tr[1]/td[5]/div/a[1]")   #删除单个商品
    quantityText=(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div[1]/table/tbody/tr[1]/td[3]/div/input")    #数量文本
    deleteAlert=(By.XPATH,"/html/body/div[17]/div/div[1]")   #删除弹窗
    refuseAlert=(By.XPATH,"#/html/body/div[18]/div/div[3]/span[1]")  #取消弹窗
    acceptAlert=(By.XPATH,"/html/body/div[18]/div/div[3]/span[2]")  #接受弹窗
    loginUrl="https://d2.shopxo.vip/login.html"
    username="yukiyuri"
    password="kmk0210012725"
    cartBtn=(By.XPATH,"/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[6]/div/a/span")


    #操作元素的方法
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver
        self.url = "https://d2.shopxo.vip/cart.html"

    def open(self,cookie):
        """打开购物车页面页面"""
        self.driver.get(self.url)
        self.driver.add_cookie(cookie_dict=cookie)
        # self.click_element(self.cartBtn)
        self.wait_for_element(self.settlementBtn)
        return self

    def is_on_login_page(self):
        """检查是否在购物车页面"""
        return "购物车" in self.driver.title

    def click_settlement(self):
        """点击结算"""
        self.click_element(self.settlementBtn)
        return self
    def click_add(self):
        """点击添加商品数量"""
        self.click_element(self.addQuantity)
        return self
    def click_sub(self):
        """点击减少商品数量"""
        self.click_element(self.subQuantity)
        return self
    def click_select_all(self):
        """点击全选"""
        self.click_element(self.selectAll)
        return self
    def click_delete_all(self):
        """点击删除所有商品"""
        self.click_element(self.deleteAll)
        return self
    def click_delete_one(self):
        """点击删除单个商品"""
        self.click_element(self.deleteOne)
        return self
    def click_refuse(self):
        """点击取消弹窗"""
        self.click_element(self.refuseAlert)
        return self
    def click_accept(self):
        """点击接受弹窗"""
        self.click_element(self.acceptAlert)
        return self
    def wait_delete_alert(self):
        """等待删除弹窗"""
        self.wait_for_element(self.deleteAlert)
        return self
    def get_quantity(self):
        """获取商品数量"""
        return self.quantityText.get_attribute("value")
    def input_quantity(self,quantity):
        """输入商品数量"""
        self.input_text(self.quantityText,quantity)
        return self

    def is_settlement_success(self):
        """是否跳转到结算页面"""
        if "订单确认" in self.driver.title:
            return True
        else :
            return False


